package com.east.service.invoice.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.east.bean.ContractPay;
import com.east.bean.Invoice;
import com.east.dao.InvoiceMapper;
import com.east.service.invoice.InvoiceService;
import com.east.utils.DateUtils;
import com.east.utils.Pagenation;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
@Service("invoiceService")
public class InvoiceServiceImpl implements InvoiceService {
	@Autowired
	InvoiceMapper invoiceMapper;
	@Override
	public Page<Invoice> findByPage(Pagenation pagenation, String key, Integer cid) {
		Page<Invoice> startPage = PageHelper.startPage(pagenation.getPageNum(), pagenation.getPageSize());
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("key", key);
		map.put("cid", cid);
		invoiceMapper.findAll(map);
		return startPage;
	}

	@Override
	public Invoice findByOne(Invoice invoice) {
		return invoiceMapper.selectByPrimaryKey(invoice.getIpId());
	}

	@Override
	public boolean saveInvoice(Invoice invoice) {
		Invoice in = new Invoice();
		in.setcType(invoice.getcType());
		in.setIpZtype(invoice.getIpZtype());
		in.setPrAcount(invoice.getPrAcount());
		in.setcId(invoice.getcId());
		if(StringUtils.isNotBlank(invoice.getDateTime())) {
			in.setPrPostTime(DateUtils.getDate(invoice.getDateTime()));
		}
		int insert = invoiceMapper.insert(in);
		if(insert>0) {
			return true;
			
		}
		return false;
	}

	@Override
	public boolean updateInvoice(Invoice invoice) {
		Invoice in = invoiceMapper.selectByPrimaryKey(invoice.getIpId());
		in.setcType(invoice.getcType());
		in.setIpZtype(invoice.getIpZtype());
		in.setPrAcount(invoice.getPrAcount());
		if(StringUtils.isNotBlank(invoice.getDateTime())) {
			in.setPrPostTime(DateUtils.getDate(invoice.getDateTime()));
		}
		int insert = invoiceMapper.updateByPrimaryKey(in);
		if(insert>0) {
			return true;
			
		}
		return false;
	}

	@Override
	public boolean delInvoice(Invoice invoice) {
		int deleteByPrimaryKey = invoiceMapper.deleteByPrimaryKey(invoice.getIpId());
		if(deleteByPrimaryKey>0) {
			return true;
		}
		return false;
	}

}
